/*-------------------------------------------------------------------------------------------------*/
/*                                                                                                 */
/* Copyright (c) 2008 Nuvoton Technology. All rights reserved.                                     */
/* All rights reserved                                                                             */
/*                                                                                                 */
/*-------------------------------------------------------------------------------------------------*/
/* File Name:                                                                                      */
/*     sc_demo.c                                                                                   */
/* Description:                                                                                    */
/*     Demo code for Non-OS/eCos smartcard driver                                                  */
/* History:                                                                                        */
/*                                                                                                 */
/* Project:                                                                                        */
/*-------------------------------------------------------------------------------------------------*/


#ifdef ECOS
#	include "stdlib.h"
#	include "string.h"
#	include "diag.h"
#	include "kapi.h"
#	include "wbio.h"
#	include "wbtypes.h"
#else
#	include <stdio.h>
#	include <stdlib.h>
#	include <string.h>
#	include "wblib.h"
#endif

#include "NUC900_smartcard.h"

#undef DBG
//#define DBG

#ifdef DBG
#	ifdef ECOS
#		define DMSG	diag_printf
#	else
#		define DMSG	sysprintf
#	endif
#else
#	define DMSG(...)
#endif

#ifdef ECOS
#	define MSG    diag_printf
#else
#	define MSG    sysprintf
#endif
/* NOTE: Commands need to be changed according to the smartcard for testing */

UINT8 GSM_T0_INC0[] = {0xA0, 0x20, 0x00, 0x01, 0x08, 0x30, 0x30, 0x30, 0x30, 0xFF, 0xFF, 0xFF, 0xFF};  /* Auth. command */
UINT8 GSM_T0_INC1[] = {0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7f, 0x10};		/* Select DF 7F10, Response 9F17 */
UINT8 GSM_T0_INC2[] = {0xA0, 0xA4, 0x00, 0x00, 0x02, 0x6f, 0x3c};		/* Select EF 6F3C, Response 9F0F */
UINT8 GSM_T0_INC3[] = {0xA0, 0xB2, 0x01, 0x04, 0xB0};					/* Read record 1 (176bytes), Response <data> 9000 */
UINT8 GSM_T0_INC4[181] = {0xA0, 0xDC, 0x01, 0x04, 0xB0};				/* write record 1 (176bytes), Response <data> 9000 */
UINT8 GSM_T0_INC5[] = {0xA0, 0xB2, 0x02, 0x04, 0xB0};					/* Read record 1 (176bytes), Response <data> 9000 */

#if 0
static void _InitSystem(void)
{

    WB_PLL_T sysClock;

    sysDisableCache();
    sysInvalidCache();
    sysSetMMUMappingMethod(MMU_DIRECT_MAPPING);
    sysEnableCache(CACHE_WRITE_BACK);

    sysClock.pll0 = PLL_166MHZ;
    sysClock.pll1 = PLL_100MHZ;
    sysClock.cpu_src = CPU_FROM_PLL0;
    sysClock.ahb_clk = AHB_CPUCLK_1_2;
    sysClock.apb_clk = APB_AHB_1_2;

    sysSetPLLConfig(&sysClock);

}
#endif
#ifdef ECOS
static VOID _ScDemo(cyg_addrword_t data)
#else
VOID _ScDemo(VOID)
#endif
{

    UINT8 atr[33];
    INT32 len, i, card;
    UINT8 buf[256];

    SC_Init();
    for (card = 1; card < 2; card++) {

        MSG("smartcard %d\n", card);

        i = SC_Open((PVOID)card);
        if (i < 0) {
            MSG("open failed\n");
            continue;
        }
        i = SC_Ioctl(card, SC_IOC_GETATR, (UINT32)atr, (UINT32)&len);
        if (i < 0) {
            MSG("ioctl failed\n");
            continue;
        } else {

            int j;
            MSG("ATR: ");
            for (j = 0; j < len; j++)
                MSG("%02x ", atr[j]);
            MSG("\n");
        }


        for (i = 5; i < 181; i++)
            GSM_T0_INC4[i] = (UINT8)i+5;
        i = SC_Write(card, GSM_T0_INC0, sizeof(GSM_T0_INC0));
        DMSG("%d\n", i);
        i = SC_Read(card, buf, 2);
        DMSG("%d\n", i);
        i = SC_Write(card, GSM_T0_INC1, sizeof(GSM_T0_INC1));
        DMSG("%d\n", i);
        i = SC_Read(card, buf, 2);
        DMSG("%d\n", i);
        i = SC_Write(card, GSM_T0_INC2, sizeof(GSM_T0_INC2));
        DMSG("%d\n", i);
        i = SC_Read(card, buf, 2);
        DMSG("%d\n", i);
        i = SC_Write(card, GSM_T0_INC4, sizeof(GSM_T0_INC4));
        DMSG("%d\n", i);
        i = SC_Read(card, buf, 2);
        DMSG("%d\n", i);
        i = SC_Write(card, GSM_T0_INC3, sizeof(GSM_T0_INC3));
        DMSG("%d\n", i);
        len = SC_Read(card, buf, 178);
        for (i = 0; i < len; i++)
            MSG("%02x ", buf[i]);


        SC_Close(card);

    }
    SC_Exit();
	
    DMSG("exit\n");
	while(1);
}

#define STACKSIZE	64 * 1024

INT main(VOID)
{

#ifdef ECOS
    cyg_handle_t    thread_handle;
    cyg_thread      thread;
    UINT8 			stack[STACKSIZE];
#endif

    //_InitSystem();

#ifdef ECOS
    cyg_thread_create(19, _ScDemo, 0, "sc_demo", stack, STACKSIZE, &thread_handle, &thread);
    cyg_thread_resume(thread_handle);
#else
    _ScDemo();
#endif


    return(0);
}
